package com.admin.education.services.Implements;

import com.admin.education.common.exceptions.ResourceNotFoundException;
import com.admin.education.dataTransferObject.PageRequest;
import com.admin.education.dataTransferObject.PageResponse;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.activity.ActivityTransfer;
import com.admin.education.dataTransferObject.activity.Mapper.ActivityMapper;
import com.admin.education.dataTransferObject.sponsor.Mapper.SponsorMapper;
import com.admin.education.dataTransferObject.sponsor.Request.CreateSponsorRequest;
import com.admin.education.dataTransferObject.sponsor.SponsorTransfer;
import com.admin.education.models.Activities;
import com.admin.education.models.Enum.SponsorStatus;
import com.admin.education.models.QSponsor;
import com.admin.education.models.Sponsor;
import com.admin.education.models.UserInformation;
import com.admin.education.repositories.SponsorRepository;
import com.admin.education.services.Interfaces.ISponsorService;
import com.querydsl.jpa.impl.JPAQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.transaction.Transactional;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@Service
@Transactional
@Validated
public class SponsorService implements ISponsorService {
    @Autowired
    private SponsorMapper sponsorMapper;
    private QSponsor qSponsor;
    @Autowired
    private SponsorRepository sponsorRepository;
    public SponsorService(){
        qSponsor =QSponsor.sponsor;

    }
    @Override
    public ServicesExcuteResult createSponsor(@Valid CreateSponsorRequest createSponsorRequest) {
        ServicesExcuteResult servicesExcuteResult = new ServicesExcuteResult();

        Sponsor sponsor = sponsorMapper.mapFrom(createSponsorRequest);

        sponsor.setCreateDate(new Date());
        servicesExcuteResult.setSuccess(true);
        this.sponsorRepository.persist(sponsor);

        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult<PageResponse<SponsorTransfer>> querySponsors(@Valid PageRequest pageRequest) {
        ServicesExcuteResult<PageResponse<SponsorTransfer>> servicesExcuteResult=new ServicesExcuteResult<>();

        JPAQuery<Sponsor> sponsorJPAQuery = this.sponsorRepository.selectFrom(qSponsor).where(qSponsor.status.eq(SponsorStatus.Normal));

        long totalCount = sponsorJPAQuery.fetchCount();

        sponsorJPAQuery = sponsorJPAQuery.orderBy(qSponsor.createDate.desc()).offset((pageRequest.getPageIndex()-1)*pageRequest.getPageSize()).limit(pageRequest.getPageSize());

        List<Sponsor> sponsors= sponsorJPAQuery.fetch();

        List<SponsorTransfer> sponsorTransfers = sponsorMapper.mapFrom(sponsors);

        PageResponse<SponsorTransfer> sponsorTransferPageResponse = new PageResponse<>(pageRequest.getPageSize(),pageRequest.getPageIndex(),(int)totalCount,sponsorTransfers);

        servicesExcuteResult.setData(sponsorTransferPageResponse);
        servicesExcuteResult.setSuccess(true);
        return servicesExcuteResult;
    }
    @Override
    public ServicesExcuteResult<PageResponse<SponsorTransfer>> querySponsorsForAudit(@Valid PageRequest pageRequest) {
        ServicesExcuteResult<PageResponse<SponsorTransfer>> servicesExcuteResult=new ServicesExcuteResult<>();

        JPAQuery<Sponsor> sponsorJPAQuery = this.sponsorRepository.selectFrom(qSponsor);

        long totalCount = sponsorJPAQuery.fetchCount();

        sponsorJPAQuery = sponsorJPAQuery.orderBy(qSponsor.createDate.desc()).offset((pageRequest.getPageIndex()-1)*pageRequest.getPageSize()).limit(pageRequest.getPageSize());

        List<Sponsor> sponsors= sponsorJPAQuery.fetch();

        List<SponsorTransfer> sponsorTransfers = sponsorMapper.mapFrom(sponsors);

        PageResponse<SponsorTransfer> sponsorTransferPageResponse = new PageResponse<>(pageRequest.getPageSize(),pageRequest.getPageIndex(),(int)totalCount,sponsorTransfers);

        servicesExcuteResult.setData(sponsorTransferPageResponse);
        servicesExcuteResult.setSuccess(true);
        return servicesExcuteResult;
    }
    @Override
    public ServicesExcuteResult<SponsorTransfer> querySponsors(long id) throws ResourceNotFoundException {
        ServicesExcuteResult<SponsorTransfer> servicesExcuteResult = new ServicesExcuteResult<>();
        JPAQuery<Sponsor> sponsorJPAQuery = this.sponsorRepository.selectFrom(qSponsor).where(qSponsor.id.eq(id));

        if(0!=sponsorJPAQuery.fetchCount())
        {
            Sponsor sponsor = sponsorJPAQuery.fetchFirst();

            servicesExcuteResult.setData(sponsorMapper.mapFrom(sponsor));
            servicesExcuteResult.setSuccess(true);
        }
        else{
            throw new ResourceNotFoundException("赞助不存在");
        }
        return servicesExcuteResult;

    }

    @Override
    public ServicesExcuteResult deleteSponsor(long id) throws ResourceNotFoundException {
        ServicesExcuteResult servicesExcuteResult = new ServicesExcuteResult();
        JPAQuery<Sponsor> sponsorJPAQuery = this.sponsorRepository.selectFrom(qSponsor).where(qSponsor.id.eq(id));

        if(0!=sponsorJPAQuery.fetchCount())
        {
            Sponsor sponsor = sponsorJPAQuery.fetchFirst();

            this.sponsorRepository.remove(sponsor);

            servicesExcuteResult.setSuccess(true);
        }
        else{
            throw new ResourceNotFoundException("赞助不存在");
        }
        return servicesExcuteResult;

    }

    @Override
    public ServicesExcuteResult auditSponsor(long id, boolean pass) throws ResourceNotFoundException {
        ServicesExcuteResult servicesExcuteResult = new ServicesExcuteResult();

        JPAQuery<Sponsor> sponsorJPAQuery = this.sponsorRepository.selectFrom(qSponsor).where(qSponsor.id.eq(id));

        if (0 != sponsorJPAQuery.fetchCount()) {
            Sponsor sponsor = sponsorJPAQuery.fetchOne();
            if (!SponsorStatus.Wait.equals(sponsor.getStatus())) {
                servicesExcuteResult.setErrorMessage("不能继续审批");
            } else {
                if (pass)
                    sponsor.setStatus(SponsorStatus.Normal);
                else
                    sponsor.setStatus(SponsorStatus.AuditNotPass);
                this.sponsorRepository.merge(sponsor);

                servicesExcuteResult.setSuccess(true);
            }
        } else {
            throw new ResourceNotFoundException("不存在该赞助信息");
        }
        return servicesExcuteResult;
    }
}
